package com.wltt.product.ahuthr.component;

import com.wltt.product.ahuthr.constants.SessionConstant;
import com.wltt.product.ahuthr.constants.SystemUserConstant;
import com.wltt.product.ahuthr.mapper.SystemUserRoleMapper;
import com.wltt.product.ahuthr.model.po.CurrentUserInfoBO;
import com.wltt.product.ahuthr.model.po.SystemUserPO;
import com.wltt.product.ahuthr.model.po.SystemUserRolePO;
import com.wltt.product.ahuthr.model.vo.ListUserSalaryRsp;
import com.wltt.product.ahuthr.utils.CodeGenerateUtil;
import com.wltt.product.ahuthr.utils.Md5Util;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class SystemUserComponent {

    @Resource
    private SystemUserRoleMapper systemUserRoleMapper;

    /**
     * 设置用户的密码，用于注册或者重置
     *
     * @param systemUserPO
     * @return
     */
    public void setPassword(SystemUserPO systemUserPO) throws NoSuchAlgorithmException {
        setPassword(Md5Util.getMD5String(SystemUserConstant.initial_password), systemUserPO);
    }

    /**
     * 设置用户的密码，用于注册或者重置
     *
     * @param systemUserPO
     * @return
     */
    public void setPassword(String checkPasswordMd5, SystemUserPO systemUserPO) throws NoSuchAlgorithmException {
        String userPasswordSalt = CodeGenerateUtil.generateUUID();
        String userPassword = Md5Util.getMD5String(checkPasswordMd5, userPasswordSalt);
        systemUserPO.setUserPassword(userPassword);
        systemUserPO.setUserPasswordSalt(userPasswordSalt);
    }

    /**
     * 设置用户的密码，用于注册或者重置
     *
     * @param systemUserPO
     * @return
     */
    public boolean checkPassword(String checkPasswordMd5, SystemUserPO systemUserPO) throws NoSuchAlgorithmException {
        String userPasswordSalt = systemUserPO.getUserPasswordSalt();
        return systemUserPO.getUserPassword().equals(Md5Util.getMD5String(checkPasswordMd5, userPasswordSalt));
    }

    /**
     * 登录成功，共享用户信息
     *
     * @param request
     * @param systemUserPO
     * @return
     */
    public void loginSuccess(HttpServletRequest request, SystemUserPO systemUserPO) throws NoSuchAlgorithmException {
        CurrentUserInfoBO currentUserInfoBO = new CurrentUserInfoBO();
        BeanUtils.copyProperties(systemUserPO, currentUserInfoBO);
        List<SystemUserRolePO> systemUserRolePOList = systemUserRoleMapper.queryBySystemUserId(currentUserInfoBO.getId());
        List<Long> roleIds = Optional.ofNullable(systemUserRolePOList).orElse(new ArrayList<>())
                        .stream().map(SystemUserRolePO::getRoleId).toList();
        currentUserInfoBO.setRoleIds(roleIds);
        request.getSession().setAttribute(SessionConstant.CURRENT_USER_INFO, currentUserInfoBO);
    }

    /**
     * 登录成功，共享用户信息
     *
     * @param request
     * @return
     */
    public CurrentUserInfoBO loginUserInfo(HttpServletRequest request) {
        Object loginInfo = request.getSession().getAttribute(SessionConstant.CURRENT_USER_INFO);
        if (null == loginInfo) {
            return null;
        }
        CurrentUserInfoBO currentUserInfoBO = new CurrentUserInfoBO();
        BeanUtils.copyProperties(loginInfo, currentUserInfoBO);
        return currentUserInfoBO;
    }

    /**
     * 登出，删除共享用户信息
     *
     * @return
     */
    public void loginOut(HttpServletRequest request) throws NoSuchAlgorithmException {
        request.getSession().invalidate();
    }
}
